package com.gxs.ea.fileupload.beans.data.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.ws.rs.core.MediaType;

import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.stereotype.Service;

import com.gxs.ea.fileportal.domain.util.ErrorResponse;
import com.gxs.ea.fileportal.domain.util.SearchParams;
import com.gxs.ea.fileportal.domain.util.SearchResults;
import com.gxs.ea.fileupload.beans.data.model.CollectionSearchResults;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.api.json.JSONConfiguration;

@Service
public class FileMetaCollectionServiceImpl implements FileMetaCollectionService
{
	private static final String BASE_RESOURCE = "http://localhost:8081/sample-rest/fileMetaCollections";
	private static Client client = null;
	static
	{
		ClientConfig clientConfig = new DefaultClientConfig();
		clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
		client = Client.create(clientConfig);
	}


	@Override
	public JSONObject insertDBObject(String companyCode,	JSONObject object) 
	{
		WebResource webResource = client.resource(BASE_RESOURCE);
		
		object.put("companyCode", companyCode);
		ClientResponse clientResponse= webResource.accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON).post(ClientResponse.class, object.toString());
		
		if(clientResponse.getStatus() == 200)
		{
			clientResponse.bufferEntity();
			String jsonString = clientResponse.getEntity(String.class);
			JSONObject jsonOBject = new JSONObject(jsonString);
			
			return jsonOBject;
		}
		else
		{
			ErrorResponse error = clientResponse.getEntity(ErrorResponse.class);
			throw new RuntimeException(error.getErrorSummary()); // Temporary handling
		}
	}

	@Override
	public CollectionSearchResults searchAll(String companyCode, int startIndex, int pageSize, String sortColumn, int sortOrder, Map<String, String> filters, Map<String, String> mapOfSearchFields) 
	{
		SearchParams s = new SearchParams();
		s.setFilters(filters);
		s.setPageSize(pageSize);
		s.setSearchFields(mapOfSearchFields);
		s.setSortField(sortColumn);
		s.setSortOrder(sortOrder);
		s.setStartIndex(startIndex);
		
		s.setCompanyCode(companyCode);

		WebResource webResource = client.resource(BASE_RESOURCE + "/searches");
		ClientResponse clientResponse= webResource.accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON).post(ClientResponse.class, s);
		
		if(clientResponse.getStatus() == 200)
		{
			
			SearchResults resultsObject = clientResponse.getEntity(SearchResults.class);
			
			CollectionSearchResults r = new CollectionSearchResults();

			r.setTotalCount(resultsObject.getTotalCount());
			r.setListOfDBObjects(resultsObject.getSearchResults() );
			return r;
		}
		else
		{
			ErrorResponse error = clientResponse.getEntity(ErrorResponse.class);
			throw new RuntimeException(error.getErrorSummary()); // Temporary handling
		}
	}
	
}
